package tech.aistar.corejava.day12.homework.home06;

import java.util.*;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/8/1
 * @Version: 1.0
 */
public class Test {
    public static void main(String[] args) {
        List<Product> list = new ArrayList<>();
        Product p1 = new Product("宝洁","洗手粉",18.5);
        Product p2 = new Product("联合利华","肥皂",4.5);
        Product p3 = new Product("宝洁","牙膏",32.5);
        Product p4 = new Product("宝洁","毛巾",14.5);
        Product p5 = new Product("洁利","洗面奶",26.0);
        Product p6 = new Product("好迪","洗发水",27.5);
        Product p7 = new Product("多芬","沐浴露",38.5);
        Product p8 = new Product("宝洁","洗洁精",3.4);

        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        list.add(p7);
        list.add(p8);

        method01(list);


    }

    /**
     * 思想 - 第三方实体类
     * 神似 - 1 1 2 1 2 4 2 1 1 2 4 5 ...
     *
     * 分析 - 统计 - 排序[数据处理]
     * 根据品牌来统计每个品牌的产品的总价.
     * @param list
     */
    private static void method01(List<Product> list) {
        //创建一个Map集合,用来保存
        //key代表的是品牌的名称
        //value是该品牌对应的所有的product对象
        Map<String,List<Product>> map = new HashMap<>();

        //遍历list集合
        //目的 - 将品牌和各自的p对应起来
        for (Product p : list) {
            //获取品牌的名称
            String brand = p.getBrand();

            //判断map集合中是否包含key - 品牌名
            if(map.containsKey(brand)){
                //根据key获取值
                List<Product> oldProds = map.get(brand);
                oldProds.add(p);
            }else{
                //创建每个品牌的集合
                List<Product> products = new ArrayList<>();
                products.add(p);
                map.put(brand,products);
            }
        }
        //创建每个品牌和他的总价
        Set<String> sets = map.keySet();
    }
}
